﻿namespace BrowserCacheBustingTests
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using ICSharpCode.SharpZipLib.Zip;

    public static class TestHelper
    {
        public static bool AreListsEqual(List<string> listOne, List<string> listTwo)
        {
            if (listOne.Count != listTwo.Count)
                return false;

            for (int i = 0; i < listOne.Count; i++)
            {
                if (!string.Equals(listOne[i], listTwo[i]))
                    return false;
            }

            return true;
        }

        public static bool UnZipFile(string zipFilePath)
        {
            bool success = true;

            try
            {
                if (File.Exists(zipFilePath))
                {
                    string baseDirectory = Path.GetDirectoryName(zipFilePath);

                    using (ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
                    {
                        ZipEntry zipEntry;

                        while ((zipEntry = ZipStream.GetNextEntry()) != null)
                        {
                            if (zipEntry.IsFile)
                            {
                                if (zipEntry.Name != "")
                                {
                                    string strNewFile = @"" + baseDirectory + @"\" + zipEntry.Name;

                                    if (File.Exists(strNewFile))
                                        continue;

                                    using (FileStream streamWriter = File.Create(strNewFile))
                                    {
                                        byte[] data = new byte[2048];
                                        while (true)
                                        {
                                            int size = ZipStream.Read(data, 0, data.Length);
                                            if (size > 0)
                                                streamWriter.Write(data, 0, size);
                                            else
                                                break;
                                        }
                                        streamWriter.Close();
                                    }
                                }
                            }
                            else if (zipEntry.IsDirectory)
                            {
                                string strNewDirectory = @"" + baseDirectory + @"\" + zipEntry.Name;

                                if (!Directory.Exists(strNewDirectory))
                                    Directory.CreateDirectory(strNewDirectory);
                            }
                        }

                        ZipStream.Close();
                    }
                }
            }
            catch
            {
                success = false;
            }

            return success;
        }

        public static void SaveResourceStreamToDisk(string resourceName, string outputPath)
        {
            using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                using (Stream output = File.Create(outputPath))
                {
                    byte[] buffer = new byte[8192];
                    int bytesRead;

                    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                        output.Write(buffer, 0, bytesRead);
                }
            }
        }

    }
}
